package no.sogl.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Membership {

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "SS")
	@Column(updatable = false, insertable = true)
	private Date createdAt = new Date();

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "SS")
	private Date updatedAt = new Date();

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "S-")
	private Date memberSince = new Date();

	@NotNull
	private boolean active = true;
	
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "S-")
	private Date activatedAt;

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "S-")
	private Date deactivatedAt;
	
	@OneToOne
    private Person person;
	
	@Size(min=1,max=30)
	private String n3sportId;
	
	@PrePersist
	@PreUpdate
	public void onUpdate() {
		updatedAt = new Date();
		if (!active && deactivatedAt == null)
			deactivatedAt = new Date();
		else if (active && activatedAt == null)
			activatedAt = new Date();
	}

}
